package com.example.chapter7.servlet;

import com.example.chapter7.bean.NewsBean;
import com.example.chapter7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 1.配置网址映射
 * 2.接受前端提交的数据库
 * 3.查询数据库（增删改查）
 *
 * 4.页面的跳转或显示（数据给前端）
 */



@WebServlet("/news.html")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String sp = req.getParameter("page");
        int page =1;
        if (sp == null && !sp.equals("")){
            page = Integer.parseInt(sp);
        }
        NewsDao newsDao = new NewsDao();
        List<NewsBean> list = newsDao.getList(page);
        req.setAttribute("list",list);
        req.getRequestDispatcher("WEB-INF/news.jsp").forward(req,resp);
    }
}